<?php

	class AppCache_Filecache extends AppCache implements Interface_AppCache
	{

		public function connect()
		{
			return true;
		}

		public function set($key, $value, $timeout = NULL, $check_key = true)
		{
			if (!$this->enabled) return true;

			$value_w = var_export($value, true);
			if (strlen($value_w) > 1000000) {
				$this->datas[$key] = $value;

				return true;
			}

			file_put_contents(CACHE_PATH . '/' . $key, '<?php $tmp = ' . $value_w . ';');

			return true;
		}

		public function sets($values, $timeout = NULL, $check_key = true)
		{
			if (!$this->enabled) return true;

			$result = true;
			foreach ($values as $key => $value)
				$result &= $this->set($key, $value, $timeout);

			return $result;
		}

		public function get($key, $check_key = true)
		{
			if (!$this->enabled) return null;

			if (isset($this->datas[$key])) {
				return $this->datas[$key];
			}

			if (!file_exists(CACHE_PATH . '/' . $key)) {
				return null;
			}

			$tmp = null;
			include(CACHE_PATH . '/' . $key);

			return $tmp;
		}

		public function gets($keys, $check_key = true)
		{
			if (!$this->enabled) return null;

			$result = true;
			foreach ($keys as $key)
				$result &= $this->get($key);

			return $result;
		}

		public function del($key)
		{
			if (!$this->enabled) return true;

			@unlink(CACHE_PATH . '/' . $key);

			return true;
		}

		public function increment($key, $value)
		{
			if (!$this->enabled) return null;

			$value = $this->get($key) + $value;
			$this->set($key, $value);

			return $value;
		}

	}

